const router = require("koa-router")();
const Mock = require("mockjs");
let { list } = Mock.mock({
  "list|100": [
    {
      img: "@image(100x100,@color)",
      title: "@ctitle(2)",
      "kj|1": ["客厅", "卧室", "餐厅", "厨房", "卫生间", "书房"],
      "fg|1": [
        "现代简约风",
        "北欧风",
        "奶油风",
        "后现代风",
        "原木风",
        "日式风",
      ],
      "pl|1": ["硬装", "家具", "灯具", "家饰", "全屋定制", "电器"],
    },
  ],
});

router.get("/list", (ctx, next) => {
  ctx.body = {
    code: 200,
    list,
  };
});
router.get("/search", (ctx) => {
  let { items } = ctx.request.query;
  let itemsList = JSON.parse(items);
  console.log(itemsList); //[ '客厅', '现代简约风', '硬装' ]
  if (itemsList.length > 0) {
    if (itemsList.length == 1) {
      let items = list.filter((item) => item.kj.includes(itemsList[0]));
      ctx.body = {
        code: 200,
        items: items,
      };
    } else if (itemsList.length == 2) {
      let items = list
        .filter((item) => item.kj.includes(itemsList[0]))
        .filter((item) => item.fg.includes(itemsList[1]));
      ctx.body = {
        code: 200,
        items: items,
      };
    } else if (itemsList.length == 3) {
      let items = list
        .filter((item) => item.kj.includes(itemsList[0]))
        .filter((item) => item.fg.includes(itemsList[1]))
        .filter((item) => item.pl.includes(itemsList[2]));
      ctx.body = {
        code: 200,
        items: items,
      };
    }
  } else {
    ctx.body = {
      items: list,
    };
  }
});
module.exports = router;
